#!/bin/bash
scriptWorkd=$(cd "$(dirname $0)";pwd)

### ========= 全局变量 ===========
### ==============================

### ========= 前置文件 ===========
if [[ -f ${HOME}/bin/basic_func.sh ]];then  ### 存在才载入
    source ${HOME}/bin/basic_func.sh
elif [[ -f ./bin/basic_func.sh ]];then
    source ./bin/basic_func.sh
elif [[ -f ../bin/basic_func.sh ]];then
    source ../bin/basic_func.sh
fi
### ==============================


function main() {
set -e
    ## 开始信息
    echo -e "${blue4}\n$(basename $0)\n${normal}"
    echo -e "${hintStrProcessing}Installing Wubi98-fcitx5-rime${normal}\n"
    sudo apt install -y curl

    ## 以下脚本来自https://gitee.com/wubi98/shell/raw/master/rime.sh
    echo -e "${hintStrProcessing}为您配置 fcitx5-rime 下的98五笔！"
    if [ -d ~/.local/autoRun98wb/fcitx5-rime ];then
      echo -e "${hintStrWarning}检测到 fcitx5-rime 文件夹已存在，执行清空！"
      rm -rf ~/.local/autoRun98wb/fcitx5-rime/*
    else
      echo -e "${hintStrWarning}检测到 fcitx5-rime 文件夹不存在，将创建这个目录！"
      mkdir -p ~/.local/autoRun98wb/fcitx5-rime
    fi
    
    curl -Lo ~/.local/autoRun98wb/fcitx5-rime/fcitx5-rime.tar.xz https://gitee.com/wubi98/fcitx5-98wb/releases/download/v2.0/fcitx5-rime.tar.xz
    
    echo -e "${hintStrOK}文件已下载！"
    
    tar -xvf ~/.local/autoRun98wb/fcitx5-rime/fcitx5-rime.tar.xz -C ~/.local/autoRun98wb/fcitx5-rime/
    
    if [ -d ~/.local/autoRun98wb/fcitx5-rime ];then
      echo -e "${hintStrProcessing}执行本地解压缩..."
      chmod -R 777 ~/.local/autoRun98wb/fcitx5-rime
      cd ~/.local/autoRun98wb/fcitx5-rime
    else
      tar -xvf ~/.local/autoRun98wb/fcitx5-rime/fcitx5-rime.tar.xz -C ~/.local/autoRun98wb/fcitx5-rime/
      chmod -R 777 ~/.local/autoRun98wb/fcitx5-rime
      cd ~/.local/autoRun98wb/fcitx5-rime
    fi
    
    file="$HOME/.local/autoRun98wb/fcitx5-rime/rime.sh"
    
    if test -e "$file"; then
        chmod 777 ~/.local/autoRun98wb/fcitx5-rime/rime.sh
        echo -e "${hintStrProcessing}配置文件安装中，请稍后..."
        bash rime.sh
        echo -e "${hintStrSucceed}配置文件安装完毕！"
    else
        echo -e "${hintStrFailed}$file 不存在\n请检查网络！"
    fi
    
    cd ~/.local/
    
    echo -e "${hintStrWarning}配置文件位于.local/share/fcitx5/fcitx5/rime内${normal}"

    ## 结束信息
    echo -e "${green4}\n$(basename $0)\n${normal}"
}
main $@
